<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Asset Manager
 *
 * Software to automatically manage how assets are delivered on a website
 *
 * @package		Asset Manager
 * @author		Adam Lewis <adam.lewis@bestservedcold.com>
 * @copyright	Copyright (c) 2006 - 2012, Best Served Cold Media Limited.
 * @license		http://bestservedcold.com/license.html
 * @link		http://bestservedcold.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Assets Library
 *
 * The Asset library simply checks the asset database and returns the asset location.  If the asset
 * is not in the database, then it calls the asset library and queues the asset for processing.
 *
 * @package		Asset Manager
 * @subpackage	Assets
 * @category	Libraries
 * @author  	Adam Lewis <adam.lewis@bestservedcold.com>
 * @since		2012-05-22
 */
abstract class Asset_manager {

	/**
	 * CI
	 *
	 * CodeIgniter super object
	 *
	 * @access	private
	 * @var 	object
	 */
	protected $CI; // The CodeIgniter super object, we'll need this

	protected $param ='';
	protected $group = FALSE;

	/**
	 * Constructor
	 *
	 * The constructor gets the CodeIgniter super object and calls in the configuration variables
	 * for use in the class.
	 *
	 * @access	public
	 * @since	2012-05-25
	 * @return	void
	 */
	public function __construct()
	{
		log_message('debug', '[ASSETS] Class initialized');
		$this->CI =& get_instance();
		$this->CI->load->config('asset_manager', TRUE);
	}

	protected function cron() {}

	protected function process_queue() {}

	protected function check_asset($asset)
	{
		// Check the base64

		// Check for optmization

		// Check for CDN

		return $asset;
	}

	/**
	 * process asset
	 *
	 * Checks if the asset exists in the database.  If it does, it returns the asset location.
	 * If it doesn't, it returns the original passed location and queues the asset for processing.
	 *
	 * @access	private
	 * @since	2012-05-25
	 * @param 	string		$file
	 * @param 	integer		$identifier
	 * @return	string
	 */
	protected function process_new_asset($file, $group = FALSE)
	{
		$this->CI->load->library('asset_manager/am_handler');
		$this->CI->build_new_asset($file, $group);
		return $this->CI->am_handler->queue_or_do();
	}

	/**
	 * Single Asset Src
	 *
	 * Okay, so the asset exists in the database, but we want to check if everything that needs doing to it is done,
	 * then queue or do it and return the relevant src string for the output handler
	 *
	 * @param unknown_type $asset
	 */
	protected function single_asset_src($asset)
	{
		log_message('error', print_r($asset, true));
		// Base64 is our first check
		if ($this->CI->config->item('base64_limit', 'asset_manager') && isset($asset['base64']) &&
			$this->CI->config->item('base64_limit', 'asset_manager') <= $asset['filesize'])
		{
			return $asset['base64'];
		} // Now see if there's a cdn file and CDN is turned on, then go get the file
		elseif (isset($asset['cdn']) && $this->CI->config->item('cdn', 'asset_manager'))
		{
			return get_cdn_url($asset['cdn']);
		} // If not, just default to the original file, which will be represented as a 0
		elseif (isset($asset['file']))
		{
			return $asset['file'];
		}
		// Log a message, somethings wrong here
		log_message('error', "[ASSET MANAGER] [AM_HANDLER] Malformed asset array [".var_dump($asset, TRUE)."]");
	}
}